package com.starsea.book.ui.newpage.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ScreenUtils
import com.blankj.utilcode.util.SizeUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.starsea.book.databinding.ItemBookCityCenterBinding
import com.starsea.book.ui.book.info.BookInfoActivity
import com.starsea.book.ui.newpage.entity.BooRecommendEntity
import com.starsea.book.ui.newpage.utils.loadNormalImage
import com.starsea.book.ui.newpage.utils.visibleOrGone
import com.starsea.book.utils.ActivityMessenger.startActivity
import com.starsea.book.utils.gone
import com.starsea.book.utils.visible

/**
 * @author Lee
 * @createTime 2024-07-12 20 GMT+8
 * @desc :
 */
class BookCityCenterAdapter(var mContext: Context) :
    BaseQuickAdapter<BooRecommendEntity, BookCityCenterAdapter.MyViewHolder>() {

    override fun onBindViewHolder(holder: MyViewHolder, position: Int, item: BooRecommendEntity?) {

        holder.binding.tvMainTitle.text = item!!.typeName
        if (!item.bookList.isNullOrEmpty())
            holder.binding.ivImage.loadNormalImage(
                mContext,
                item.bookList?.first()?.bookCover ?: ""
            )
        val layoutParams = holder.binding.cvImg.layoutParams as ConstraintLayout.LayoutParams
//
//        layoutParams.width = (ScreenUtils.getScreenWidth() * 72 / 375.0).toInt()
        layoutParams.width =
            ((ScreenUtils.getScreenWidth() - SizeUtils.dp2px(24f + 14f + 4 * 12f)) / 4.0).toInt()
//
        layoutParams.height =
            ((layoutParams.width) * 96.0 / 72.0).toInt()

        holder.binding.cvImg.layoutParams = layoutParams
        if (!item.bookList.isNullOrEmpty())
            holder.binding.tvTitle.text = item.bookList?.first()?.bookTitle
        if (!item.bookList.isNullOrEmpty())
            holder.binding.tvDesc.text = item.bookList?.first()?.bookDesc

//    0连载1完结2停更
        if (!item.bookList.isNullOrEmpty())
            holder.binding.tvHint.text = "${
                if (item.bookList?.first()?.bookState == 0) "连载"
                else if (item.bookList?.first()?.bookState == 1) "完结"
                else "停更"
            }·${item.bookList?.first()?.wordCount}"

        if (!item.bookList.isNullOrEmpty())
            holder.binding.rtvVipTag.visibleOrGone(item.bookList?.first()?.isVipBook == 1)

        holder.binding.rvContent.layoutManager =
//            LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false)
            GridLayoutManager(mContext, 4)
        if (item.bookList!!.size > 1) {
            holder.binding.rvContent.visible()
            val mAdapter = BookCityTopAdapter(mContext)
            holder.binding.rvContent.adapter = mAdapter
            val list = item.bookList!!.subList(1, item.bookList!!.size)
            mAdapter.submitList(list)
            mAdapter.setOnItemClickListener { adapter, view, position ->
                startActivity(
                    mContext,
                    BookInfoActivity::class,
                    "bookId" to mAdapter.items[position].bookId!!
                )
            }
        } else {
            holder.binding.rvContent.gone()
        }
    }

    override fun onCreateViewHolder(
        context: Context,
        parent: ViewGroup,
        viewType: Int,
    ): MyViewHolder {
        return MyViewHolder(parent = parent)
    }

    class MyViewHolder(
        parent: ViewGroup,
        val binding: ItemBookCityCenterBinding = ItemBookCityCenterBinding.inflate(
            LayoutInflater.from(parent.context), parent, false
        ),
    ) : RecyclerView.ViewHolder(binding.root)

}